package cn.tedu.eb_film.controller;




import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.pojo.dto.*;
import cn.tedu.eb_film.pojo.entity.User;
import cn.tedu.eb_film.pojo.vo.UserJwtInfo;
import cn.tedu.eb_film.pojo.vo.UserListItemVO;
import cn.tedu.eb_film.pojo.vo.UserStandardVO;
import cn.tedu.eb_film.service.UserService;
import cn.tedu.eb_film.utils.Captcha;
import cn.tedu.eb_film.utils.JsonResult;
import cn.tedu.eb_film.utils.ServiceCode;
import cn.tedu.eb_film.utils.SmsUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author HおE
 * @since 2023-03-14 18:38:25
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理模块")
@Slf4j
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;


    /**
     * 新增数据
     *
     * @param userAddNewDTO 实体
     * @return 新增结果
     */
    @ApiOperation("添加用户")
    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始执行添加业务，参数为：{}", userAddNewDTO);
        if (userAddNewDTO.getUsername() == null ||
            "".equals(userAddNewDTO.getUsername()) ||
             userAddNewDTO.getPassword() == null ||
            "".equals(userAddNewDTO.getPassword()) ||
            userAddNewDTO.getPhone() == null ||
             userAddNewDTO.getPhoneCode() == null
        ){
            String message = "添加用户失败，数据不能为空！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE, message);
        }
           userService.insert(userAddNewDTO);
        return JsonResult.ok();
    }

    /**
     * 用户账号密码登录
     *
     * @param userLoginDto 实体
     * @return 新增结果
     */
    @ApiOperation("用户账号密码登录")
    @PostMapping("/accountLogin")
    @ApiOperationSupport(order = 110)
    public JsonResult<UserJwtInfo> accountLogin(@RequestBody UserLoginDto userLoginDto) {
        log.debug("开始执行账号密码登录业务，参数为：{}", userLoginDto);
        if (userLoginDto.getUsername() == null ||
                "".equals(userLoginDto.getUsername()) ||
                userLoginDto.getPassword() == null ||
                "".equals(userLoginDto.getPassword())
        ){
            String message = "登录用户失败，数据不能为空！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE, message);
        }
        UserJwtInfo userJwtInfo = userService.accountLogin(userLoginDto);
        return JsonResult.ok(userJwtInfo);
    }



    /**
     * 用户账号密码登录
     *
     * @param userAddNewDTO 实体
     * @return 新增结果
     */
    @ApiOperation("用户手机号登录")
    @PostMapping("/phoneLogin")
    @ApiOperationSupport(order = 120)
    public JsonResult<Void> phoneLogin(UserAddNewDTO userAddNewDTO) {
        log.debug("开始执行手机号登录业务，参数为：{}", userAddNewDTO);
        if (userAddNewDTO.getPhone() == null ||
                "".equals(userAddNewDTO.getPhone()) ||
                userAddNewDTO.getPhoneCode() == null ||
                "".equals(userAddNewDTO.getPhoneCode())
        ){
            String message = "登录用户失败，数据不能为空！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE, message);
        }
        userService.phoneLogin(userAddNewDTO);
        return JsonResult.ok();
    }




    /**
     * 发送手机号验证码
     * @param phone
     * @return
     */
    @ApiOperation("发送验证码")
    @PostMapping("/{phone:[0-9]+}/seedCode")
    @ApiImplicitParam(name = "phone", value = "电话号码", required = true, dataType = "String")
    public JsonResult<Void> seedCode(@PathVariable String phone){
        log.debug("开始处理【根据手机号发送验证码】的请求，参数：{}", phone);
        userService.seedCode(phone);
        return JsonResult.ok();
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/{id:[0-9]+}/deleteById")
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult<Void> deleteById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！")Long id) {
        log.debug("开始处理【根据ID删除类别】的请求，参数：{}", id);
        userService.deleteById(id);
        return JsonResult.ok();
    }


    /**
     * 修改数据
     *
     * @param userUpdateDTO 实体
     * @return 编辑结果
     */
    @PostMapping("/{id:[0-9]+}/updateById")
    @ApiOperation("修改用户")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！")Long id,
                                            UserUpdateDTO userUpdateDTO) {
        userService.updateInfoById(id,userUpdateDTO);
        return JsonResult.ok();
    }


    @PostMapping("/see")
    @ApiOperation("想看电影人数")
    public JsonResult<Void>see(@RequestBody UserSeeDTO userSeeDTO){
        log.debug("userSeeDTO{}",userSeeDTO);
        userService.see(userSeeDTO);
        return JsonResult.ok();
    }





    // 启用类别
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    // 禁用类别
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        userService.setDisable(id);
        return JsonResult.ok();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id:[0-9]+}/selectById")
    @ApiOperation("查询单个用户")
    @ApiOperationSupport(order = 400)
    public JsonResult<UserStandardVO> updateById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        UserStandardVO userStandardVO = userService.selectById(id);
        return JsonResult.ok(userStandardVO);
    }

    /**
     * 通过特殊字段查询单条数据
     *
     * @param userLikeDTO 特殊字段
     * @return 单条数据
     */
    @PostMapping("/selectByUserLike")
    @ApiOperation("通过特殊字段查询用户")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<UserListItemVO>> selectByUserLike(UserLikeDTO userLikeDTO) {
        List<UserListItemVO> list = userService.selectByLike(userLikeDTO);
        return JsonResult.ok(list);
    }

    /**
     * 通过主键查询数据列表
     *
     * @return 数据列表
     */
    @GetMapping("/selectList")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 410)
    public JsonResult<List<UserListItemVO>> updateList() {
        List<UserListItemVO> list = new ArrayList<>();
        list =  userService.selectList();
        return JsonResult.ok(list);
    }
    /**
     * 判断手机号是否注册
     *
     * @return 数据列表
     */
    @ApiOperation("判断手机号是否注册")
    @PostMapping("/{phone:[0-9]+}")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParam(name = "phone", value = "电话号码", required = true, dataType = "String")
    public JsonResult<Void> phone(@PathVariable String phone){
        log.debug("开始处理【判断手机号是否注册】的请求，参数：{}", phone);
        userService.countByPhone(phone);
        return JsonResult.ok();
    }
}

